package tests;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class HomePage   {
    public static WebDriver driver ;

    public HomePage(WebDriver driver){
        HomePage.driver = driver ;
    }

   // 搜索好友
    public void search() throws InterruptedException {
         driver.findElement(By.cssSelector("#search > input[type=text]")).clear();
         driver.findElement(By.cssSelector("#search > input[type=text]")).sendKeys("zhangsan");
         driver.findElement(By.cssSelector("#search > button")).click();

         // 看一下是不是zhangsan

        String name = driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > div > h4")).getText();

        assert "zhangsan".equals(name);
        driver.findElement(By.cssSelector("#friend-list > li:nth-child(1)")).click();
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        alert.accept();
        // 注销
        exit();
       // 切换到张三的账号
        loginSuccess();

        // 同意好友请求
        driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > div > button:nth-child(3)")).click();
        Thread.sleep(200);
        driver.switchTo().alert().accept();
        exit();
    }

    // 搜索不存在的好友
    public void searchFail() throws InterruptedException {
        driver.findElement(By.cssSelector("#search > input[type=text]")).clear();
        driver.findElement(By.cssSelector("#search > input[type=text]")).sendKeys("zhangsan123");
        driver.findElement(By.cssSelector("#search > button")).click();

        // 看一下是不是zhangsan

        String name = driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > div > h4")).getText();

        assert "zhangsan".equals(name);
    }

    // 登录
    public void loginSuccess() throws InterruptedException {

        // 先清空输入框
        driver.findElement(By.cssSelector("#userName > input[type=text]")).clear();
        driver.findElement(By.cssSelector("#inputCaptcha")).clear();
        driver.findElement(By.cssSelector("#password > input[type=password]")).clear();
        // 输入正确用户名+密码+万能验证码
        driver.findElement(By.cssSelector("#userName > input[type=text]")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password > input[type=password]")).sendKeys("123");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("xieyeqi");
        // 登录
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(1000);
        // 判断当前是否是成功登录的主页
        String title =  driver.getTitle();
        assert "主页".equals(title) ;
        // 退出
    }

    // 不选择好友发送消息
    public void sendMessageFail() throws InterruptedException {
        driver.findElement(By.cssSelector("#message-input")).sendKeys("666");
        driver.findElement(By.cssSelector("#send > button")).click();
        Thread.sleep(400);
        String text = driver.switchTo().alert().getText();

        assert "请先选择好友".equals(text) ;
    }

    // 发送消息
    public void sendMessage() throws InterruptedException {
        driver.findElement(By.cssSelector("#friend-table")).click();
        Thread.sleep(500);
        WebElement element =  driver.findElement(By.cssSelector("#friend-list > li:nth-child(1)"));

        Actions actions = new Actions(driver);
         actions.moveToElement(element).click().perform();

        driver.findElement(By.cssSelector("#message-input")).sendKeys("666");
        driver.findElement(By.cssSelector("#send > button")).click();
         Thread.sleep(400);
        String text =  driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > div.list-li > p")).getText();

        assert "666".equals(text) ;

    }
    // 删除好友
    public void deleteFriend() throws InterruptedException {
        driver.findElement(By.cssSelector("#deleteUser")).click();
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert() ;
        String text = alert.getText() ;
        assert  text.equals("是否删除这位好友");
        alert.accept();
        Thread.sleep(500);
        assert "删除成功".equals(alert.getText()) ;
        alert.accept();
    }
    // 不选择好友就 删除好友
    public void deleteFriendFail() throws InterruptedException {
        driver.findElement(By.cssSelector("#deleteUser")).click();
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert() ;
        String text = alert.getText() ;
        assert  text.equals("请先选择要删除的好友");

    }
    // 注销
    public void exit() throws InterruptedException {
        driver.findElement(By.cssSelector("#setting > img")).click();
        driver.findElement(By.cssSelector("#setting > button")).click();
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert() ;
        assert "是否退出登录".equals(alert.getText());
        alert.accept();
        Thread.sleep(500);
        assert "登录".equals(driver.getTitle()) ;
    }

}
